/**
 * Created by happy on 2017/9/4.
 */

var  notifCodeNum;
var baseUrl="http://localhost:8686";
/******
 * 注册或绑定微信
 */
function bindOrRegister(){
    var mobile=$("#mobilephone").val();
    var openid=getQueryString("wechat");
    if (!mobile||mobile.length!=11){
        alert("请输入正确手机号");
        return;
    }
    var vcode=$("#verfiyCodeIpt").val();
    if (!vcode){
        alert("请输入正确验证码");
        return;
    }
    var pwd=$("#pwd").val();
    var pwd2=$("#pwd2").val();
    if (!pwd){
        alert("请输入密码");
        return;
    }
    if (pwd!=pwd2)
    {
        alert("密码不一致");
        return;
    }

    $.ajax( {
        url:baseUrl+"/api/bindOrRegister",// 跳转到 action
        data:{
            mobilephone :mobile,
            passWord : pwd,
            wechat:openid
        },
        type:'post',
        cache:false,
        dataType:'json',
        success:function(data) {
            alert(data);
            console.log(JSON.stringify(data));
            if (data.data.isRealname!=1){
                window.location.href="http://shucoins.iok.la/wx_realname.html?wechat="+openid+"&part=1";
                return;
            }
            if (!data.data.paymentPwd){
                window.location.href="http://shucoins.iok.la/wx_paypw.html?wechat="+openid+"&part=2";
                return;
            }
            okSubmit("您已成功绑定帐号！");
            window.close();
            aWeixinJSBridge.call('closeWindow');
        },
        error : function() {
            alert("异常！");
            console.log("异常！");
        }
    });
}

// function doUpload() {
//     loadingWinForm();
//     setTimeout("uploadImg()",1000);
// }
function uploadImg(){
    var wechat=getQueryString("wechat");
    $("#wechat").val(wechat);
    if (!wechat){
       alert("地址错误");
       return;
    }
    var formData = new FormData($("#uploadForm")[0]);
    $ .ajax({
       url : baseUrl+'/api/uploadIdCard',
        // url :  baseUrl+'/uploadIdCard',
        type : 'POST',
        data : formData,
        async : false,
        cache : false,
        contentType : false,
        processData : false,
        success : function(data) {
            if (data.code == 200) {
                alert(JSON.stringify(data));
                showPart(2);
             //   bindUserData(data);
            }else if (data.code == 4001) {
                alert(data["massage"]);
            }else if (data.code == 4002) {
                top.window.location.href="index.html";
            }else if (data.code == 4006) {
                alert(data["massage"]);
            }else if (data.code == 4007) {
                alert(data["massage"]);
            }
        },
        error : function(error) {
            alert("error");
        }
    });
}

function updateUserInfo() {
    var formData = new FormData($("#centercenterfailones_form")[0]);
    $.ajax({
        url : baseUrl+'/api/updateUserInfo',
        type : 'POST',
        data : formData,
        async : false,
        cache : false,
        contentType : false,
        processData : false,
        success : function(data) {
            if (data.code == 200) {
                alert(JSON.stringify(data));
                var popUptext = "恭喜你实名认证成功";
                parent.parent.window.parentOkSubmit(popUptext);
                window.setTimeout(function() {
                    parent.parent.window.popUpSubmit();

                }, 1000);
                window.location.reload(true);
            }
        },
        error : function(error) {
            alert(error);
        }
    });
}












function resetPayPw() {
    var new_passwd=$("#new_passwd").val();
    var new_passwd2=$("#new_passwd2").val();
    if (new_passwd!=new_passwd2){
        alert("新密码不一致");
        $("#informal").html("新密码不一致");
        return
    }
    if (new_passwd.length<8){
        alert("新密码长度不能小于8");
        $("#informal").html("新密码长度不能小于8");
    }
    $.ajax({
        url : baseUrl+'/api/setPayPasswd',// 跳转到 action
        data:{
            'new_password' : new_passwd,
            'new_password2':new_passwd2,
            'wechat':getQueryString('wechat')
        },
        type:'post',
        cache:false,
        dataType:'json',
        success : function(data) {
            switch (data.code){
                case 200:
                    console.log(JSON.stringify(data));
                    okSubmit("恭喜你的交易密码修改成功");
                    break;
                case 4009:
                case 4010:
                case 4011:
                    break;
            }
        },
        error : function() {
            console.log("异常");
        }
    });
}




/********
 * 发送验证码
 */


function sendVCode() {
    var mobile=$("#mobilephone").val();
    if (!mobile||mobile.length!=11){
        alert("请输入正确手机号");
        return;
    }

    $.ajax({
        url : baseUrl+"/api/sendVCode",// 跳转到 action
        data:{mobile:mobile},
        type : 'post',
        async : true,
        cache : false,
        beforeSend: function(){
            showTime(60, "promptRestriction");
        },
        success : function(data) {
            switch (data.code){
                case 200:
                    console.log(JSON.stringify(data));
                    okSubmit("短信验证码已发到手机请查收");
                    break;
                case 4009:
                case 4010:
                case 4011:
                    showTime('0');
                    break
            }
        },
        error : function() {
            console.log("异常");
        }
    });
}


function bindUserData(data) {
    document.getElementById("username").value = data.data.name;
    document.getElementById("usernumber").value = data.data.idcard;
    document.getElementById("address").value = data.data.address;
    if (data.data.sex == "男") {
        document.getElementById('sex').value = "1";
    } else {
        document.getElementById('sex').value = "0";
    }
    document.getElementById("startdate").value = data.data.validDate+"".split("-")[0];
    document.getElementById("enddate").value = data.datavalidDate+"".split("-")[1];


}

function showPart(part){
    if (!part){
        if (!frist){
            part=getQueryString("part");
            frist=true;
        }
        return;
    }
    $("#uploadForm").hide();
    $("#centercenterfailones_form").hide();
    $("#form_pay_pw").hide();
    switch (part){
        case 1:
            $("#uploadForm").show();
            break;
        case 2:
            $("#centercenterfailones_form").show();
            break;
        case 3:
            break;
        default:
//                $("#centercenterfailones_form").hide();
//                $("#form_pay_pw").hide();

    }
}


